//
//  UITool.m
//  UU
//
//  Created by 王 家振 on 13-11-2.
//  Copyright (c) 2013年 王 家振. All rights reserved.
//

#import "UITool.h"
#import "ToolColor.h"
#import "NSString+SelfSize.h"
@implementation UITool

+ (void) showSampleMsg:(NSString *)title message:(NSString *) strMsg
{
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:strMsg
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
}

// MBProgressHUD 文本提示框
+ (void) showMBProgressHud:(UIView* )view withText:(NSString* )str withTime:(CGFloat)time
{
    MBProgressHUD* proHUD = [[MBProgressHUD alloc] initWithView:view];
    [view addSubview:proHUD];
    proHUD.mode = MBProgressHUDModeText;
    proHUD.labelText = str;
    proHUD.labelFont = [UIFont systemFontOfSize:15];
    [proHUD show:YES];
    [proHUD hide:YES afterDelay:time];
}

+ (void) showMBProgressHud:(UIView *)view withTitle:(NSString *)str detail:(NSString* )detail withTime:(CGFloat)time
{
    MBProgressHUD* proHUD = [[MBProgressHUD alloc] initWithView:view];
    [view addSubview:proHUD];
    proHUD.mode = MBProgressHUDModeText;
    proHUD.labelText = str;
    proHUD.detailsLabelText = detail;
    [proHUD show:YES];
    [proHUD hide:YES afterDelay:time];
//    [proHUD release];
}

+ (void) showMBProgressHud:(UIView *)view withTitle:(NSString *)str detail:(NSString* )detail withTime:(CGFloat)time completion:(void(^)())block
{
    MBProgressHUD* proHUD = [[MBProgressHUD alloc] initWithView:view];
    [view addSubview:proHUD];
    proHUD.completionBlock = block;
    proHUD.mode = MBProgressHUDModeText;
    proHUD.labelText = str;
    proHUD.detailsLabelText = detail;
    [proHUD show:YES];
    [proHUD hide:YES afterDelay:time];
//    [proHUD release];
}

//+ (void)showLoadProgressHud:(UIView *)view withText:(NSString *)str animated:(BOOL)animated
//{
//    XHFriendlyLoadingView *viewTemp = [[XHFriendlyLoadingView alloc] initWithFrame:CGRectMake(0, 0, 100*XH_SCALE_FACTOR, 100*XH_SCALE_FACTOR)];
//
//    viewTemp.layer.cornerRadius = 5;
////    viewTemp.backgroundColor = [UIColor clearColor];
//    [view addSubview:viewTemp];
//    viewTemp.center = CGPointMake(view.frame.size.width/2, view.frame.size.height/2);
//    [viewTemp showFriendlyLoadingViewWithText:str loadingAnimated:animated];
//    [viewTemp release];
//    
//}
//
//+ (void)hideLoadProgressHud:(UIView *)view
//{
//    UIView *viewTemp = nil;
//    for (int i = 0; i<view.subviews.count; i++) {
//        id obj = [view.subviews objectAtIndex:i];
//        if([obj isKindOfClass:[XHFriendlyLoadingView class]])
//        {
//            viewTemp = obj;
//            break;
//        }
//    }
//    if(viewTemp)
//    {
//        [viewTemp removeFromSuperview];
//    }
//}

// 不需要委托alert
+ (void) alertNotifyError:(NSString* )title contentStr:(NSString* )content thread:(BOOL)isOnMainThread
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:content
                                                       delegate:nil
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确定", nil];
    if (isOnMainThread)
    {
        [alertView show];
    }
    else
    {
        [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
    }
}

// 只要一个确定按钮
+ (void) alertNotify:(NSString* )title contentStr:(NSString* )content thread:(BOOL)isOnMainThread
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:content delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    
    if (isOnMainThread)
    {
        [alertView show];
    }
    else
    {
        [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
    }
}

// 有确认，取消两个按钮的Alert
+ (void) alertOkAndCancel:(NSString* )title contentStr:(NSString* )content delegate:(id)target thread:(BOOL)isOnMainThread
{
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:content
                                                       delegate:target
                                              cancelButtonTitle:@"确认"
                                              otherButtonTitles:@"取消",nil];
    if (isOnMainThread)
    {
        [alertView show];
    }
    else
    {
        [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
    }
}

+ (void) alertOkAndCancel:(NSString* )title contentStr:(NSString* )content delegate:(id)target thread:(BOOL)isOnMainThread withTag:(int)tag
{
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:content
                                                       delegate:target
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确认",nil];
    alertView.tag = tag;
    if (isOnMainThread)
    {
        [alertView show];
    }
    else
    {
        [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
    }
}

// 不需要回调时target设为nil即可
+ (void) showMessage:(NSString* )msg titleStr:(NSString* )title delegate:(id)target
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:target cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
    [alert show];
}

#pragma mark - 自定义 导航条 按钮
+ (void)customizeNavBar:(UIViewController*)targetView withTitle:(NSString *)title
            haveLButton:(BOOL)bHaveL haveRButton:(BOOL)bHaveR
       normalImageNameL:(NSString*)nmlImgNameL highlightedImageNameL:(NSString*)hltImgNameL buttonNameL:(NSString*)btnNameL
       normanImageNameR:(NSString*)nmlImgNameR highlightedImageNameR:(NSString*)hltImgNameR buttonNameR:(NSString*)btnNameR
{
    // 有左边的按钮
    if (bHaveL)
    {
        // 设置左边按钮
        UIImage *normalImage = [UIImage imageNamed:nmlImgNameL];
        UIImage *scaledImage=[UIImage imageWithCGImage:normalImage.CGImage scale:2 orientation:UIImageOrientationUp];
        normalImage=[scaledImage stretchableImageWithLeftCapWidth:scaledImage.size.width/2 topCapHeight:scaledImage.size.height/2];
        
        UIImage* downImage = [UIImage imageNamed:hltImgNameL];
        UIImage* downScaleImage = [UIImage imageWithCGImage:downImage.CGImage scale:2 orientation:UIImageOrientationUp];
        //downImage = [downScaleImage stretchableImageWithLeftCapWidth:scaledImage.size.width/2 topCapHeight:scaledImage.size.height/2];
        
        UIButton* leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, normalImage.size.width-2, normalImage.size.height)];
        [leftButton setTitle:btnNameL forState:UIControlStateNormal];
        leftButton.titleLabel.font = [UIFont systemFontOfSize:BTN_TITLE_SIZE];
        [leftButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        leftButton.titleEdgeInsets=UIEdgeInsetsMake(0, 7, 0, 0);
        [leftButton setBackgroundImage:normalImage forState:UIControlStateNormal];
        [leftButton setBackgroundImage:downScaleImage forState:UIControlStateHighlighted];
        [leftButton addTarget:targetView action:@selector(leftBtnMethod) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem* leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
        targetView.navigationItem.leftBarButtonItem = leftBarItem;

    }
    
    // 有右边的按钮
    if (bHaveR)
    {
        // 设置右边按钮
        UIImage* normalImage = [UIImage imageNamed:nmlImgNameR];
        UIImage* scaledImage=[UIImage imageWithCGImage:normalImage.CGImage scale:2 orientation:UIImageOrientationUp];
        normalImage=[scaledImage stretchableImageWithLeftCapWidth:scaledImage.size.width/2 topCapHeight:scaledImage.size.height/2];
        
        UIImage* downImage = [UIImage imageNamed:hltImgNameR];
        UIImage* downScaleImage = [UIImage imageWithCGImage:downImage.CGImage scale:2 orientation:UIImageOrientationUp];
        //downImage = [downScaleImage stretchableImageWithLeftCapWidth:scaledImage.size.width/2 topCapHeight:scaledImage.size.height/2];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBtn.frame = CGRectMake(0, 0, normalImage.size.width-2, normalImage.size.height);
        [rightBtn setBackgroundImage:normalImage forState:UIControlStateNormal];
        [rightBtn setBackgroundImage:downScaleImage forState:UIControlStateHighlighted];
        [rightBtn setTitle:btnNameR forState:UIControlStateNormal];
        [rightBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        rightBtn.titleLabel.font = [UIFont systemFontOfSize:BTN_TITLE_SIZE];
        rightBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 4, 0, 0);
        [rightBtn addTarget:targetView action:@selector(rightBtnMethod) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        targetView.navigationItem.rightBarButtonItem=rightBarButtonItem;
    }
    
    // 设置导航条标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 220, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor darkGrayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = title;
    targetView.navigationItem.titleView = titleLabel;
}

#pragma mark - 设置导航条, 左边为凌形按钮，右边为长方形按钮
+ (void)customizeNavBar:(UIViewController*)targetView withTitle:(NSString *)title buttonNameL:(NSString*)btnNameL buttonNameR:(NSString*)btnNameR
{
    // 有左边的按钮
    if (btnNameL)
    {
        // 设置左边按钮
        UIImage *normalImage = [UIImage imageNamed:@"back_normal.png"];
        UIImage *scaledImage=[UIImage imageWithCGImage:normalImage.CGImage scale:2 orientation:UIImageOrientationUp];
        normalImage=[scaledImage stretchableImageWithLeftCapWidth:scaledImage.size.width/2 topCapHeight:scaledImage.size.height/2];
        
        UIImage* downImage = [UIImage imageNamed:@"back_push.png"];
        UIImage* downScaleImage = [UIImage imageWithCGImage:downImage.CGImage scale:2 orientation:UIImageOrientationUp];
        //downImage = [downScaleImage stretchableImageWithLeftCapWidth:scaledImage.size.width/2 topCapHeight:scaledImage.size.height/2];
        
        UIButton* leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, normalImage.size.width-2, normalImage.size.height)];
        [leftButton setTitle:btnNameL forState:UIControlStateNormal];
        leftButton.titleLabel.font = [UIFont systemFontOfSize:BTN_TITLE_SIZE];
        [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        leftButton.titleEdgeInsets=UIEdgeInsetsMake(0, 7, 0, 0);
        [leftButton setBackgroundImage:normalImage forState:UIControlStateNormal];
        [leftButton setBackgroundImage:downScaleImage forState:UIControlStateHighlighted];
        [leftButton addTarget:targetView action:@selector(leftBtnMethod) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem* leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
        targetView.navigationItem.leftBarButtonItem = leftBarItem;

    }
    
    // 有右边的按钮
    if (btnNameR)
    {
        // 设置右边按钮
        UIImage* normalImage = [UIImage imageNamed:@"btn_nav_next_normal"];
        UIImage* scaledImage=[UIImage imageWithCGImage:normalImage.CGImage scale:2 orientation:UIImageOrientationUp];
        normalImage=[scaledImage stretchableImageWithLeftCapWidth:scaledImage.size.width/2 topCapHeight:scaledImage.size.height/2];
        
        UIImage* downImage = [UIImage imageNamed:@"btn_nav_next_push.png"];
        UIImage* downScaleImage = [UIImage imageWithCGImage:downImage.CGImage scale:2 orientation:UIImageOrientationUp];
        //downImage = [downScaleImage stretchableImageWithLeftCapWidth:scaledImage.size.width/2 topCapHeight:scaledImage.size.height/2];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBtn.frame = CGRectMake(0, 0, normalImage.size.width-2, normalImage.size.height);
        [rightBtn setBackgroundImage:normalImage forState:UIControlStateNormal];
        [rightBtn setBackgroundImage:downScaleImage forState:UIControlStateHighlighted];
        //加载的是图片
        if ([btnNameR rangeOfString:@".png"].location != NSNotFound)
        {
            UIImageView *btnImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 29, 25)];
            UIImage* normalImage = [UIImage imageNamed:btnNameR];
            btnImage.image = normalImage;
            btnImage.center = rightBtn.center;
            btnImage.backgroundColor = [UIColor clearColor];
            [rightBtn addSubview:btnImage];
            
        }else{
            [rightBtn setTitle:btnNameR forState:UIControlStateNormal];
            [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            rightBtn.titleLabel.font = [UIFont systemFontOfSize:BTN_TITLE_SIZE];
            rightBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 4, 0, 0);
        }
        [rightBtn addTarget:targetView action:@selector(rightBtnMethod) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        targetView.navigationItem.rightBarButtonItem=rightBarButtonItem;
    }
    
    // 设置导航条标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 220, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:21];
    titleLabel.text = title;
    targetView.navigationItem.titleView = titleLabel;
}

- (void)rightBtnMethod
{
    //NSLog(@"rightBtnMethod\n");
}

- (void)leftBtnMethod
{
    //NSLog(@"leftBtnMethod\n");
}

// 设置导航条,左右都是图片按钮
+ (void)customizeNavBar:(UIViewController* )target withTitle:(NSString* )title buttonImgL:(UIView* )left buttonImgR:(UIView* )right
{
    float w1 = 0;
    float w2 = 0;
    if (left)
    {
        target.navigationItem.leftBarButtonItem.width = left.frame.size.width;
        UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithCustomView:left];
        target.navigationItem.leftBarButtonItem = leftItem;
        w1 = left.frame.size.width*2;//target.navigationItem.leftBarButtonItem.width;
    }
    
    if (right)
    {
        target.navigationItem.rightBarButtonItem.width = right.frame.size.width;
        UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:right];
        target.navigationItem.rightBarButtonItem = rightItem;
        w2 = right.frame.size.width;//target.navigationItem.rightBarButtonItem.width;
    }
    
    if (title)
    {
        float tmpW = 0;
        if (w1 > w2)
        {
            tmpW = w1;
        }
        else
        {
            tmpW = w2;
        }
        float w = [UIScreen mainScreen].bounds.size.width - tmpW*2;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, 44)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont boldSystemFontOfSize:21];
        titleLabel.text = title;
        target.navigationItem.titleView = titleLabel;
    }
    
}

+ (void)customizeNavBar:(UIViewController* )target withTitle:(NSString *)title buttonLeft:(UIButton *)leftBtn leftImage:(UIImage *)leftImage leftTitle:(NSString *)leftTitle
{
    CGFloat fontSize = 18.0;
    if (leftBtn)
    {
        if(leftImage)
        {
            CGSize sizeTemp = leftImage.size;
            leftBtn.frame = CGRectMake(0, 0, 44, 44);
            CGFloat top = (44 -fontSize)/2;
            CGFloat left = 44 - fontSize*sizeTemp.width/sizeTemp.height;
            [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(top, 0, top, left)];
            [leftBtn setImage:leftImage forState:UIControlStateNormal];
        }
        
        if(leftTitle)
        {
            leftBtn.frame = CGRectMake(0, 0, [leftTitle getSizeFromSelfWithWidth:320 andFont:18.].width+5, 44);
            [leftBtn setTitle:leftTitle forState:UIControlStateNormal];
        }
        
        UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
        target.navigationItem.leftBarButtonItem = leftItem;
    }
    
    if (title)
    {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100., 44)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.backgroundColor = [UIColor clearColor];
        [titleLabel setFont:[UIFont systemFontOfSize:17.]];
        titleLabel.textColor = [UIColor colorWithRed:119./255.0 green:119./255.0 blue:119./255.0 alpha:1.];
        titleLabel.text = title;
        target.navigationItem.titleView = titleLabel;
    }
    
    
}
//// 设置导航条，标题与右边按钮
+ (void)customizeNavBar:(UIViewController* )target withTitle:(NSString *)title buttonRight:(UIButton *)rightBtn rightImage:(UIImage *)rightImage rightTitle:(NSString *)rightTitle
{
    CGFloat fontSize = 18.0;
    if (rightBtn)
    {
        if(rightImage)
        {
            CGSize sizeTemp = rightImage.size;
            rightBtn.frame = CGRectMake(0, 0, 44, 44);
            CGFloat top = (44 -fontSize)/2;
            CGFloat left = 44 - fontSize*sizeTemp.width/sizeTemp.height;
            [rightBtn setImageEdgeInsets:UIEdgeInsetsMake(top, 0, top, left)];
            [rightBtn setImage:rightImage forState:UIControlStateNormal];
        }
        
        if(rightTitle)
        {
            rightBtn.frame = CGRectMake(0, 0, [rightTitle getSizeFromSelfWithWidth:320 andFont:18.].width+5, 44);
            [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
        }
        
        UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        target.navigationItem.rightBarButtonItem = rightItem;
    }
    
    if (title)
    {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100., 44)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.backgroundColor = [UIColor clearColor];
        [titleLabel setFont:[UIFont systemFontOfSize:17.]];
        titleLabel.textColor = [UIColor colorWithRed:119./255.0 green:119./255.0 blue:119./255.0 alpha:1.];
        titleLabel.text = title;
        target.navigationItem.titleView = titleLabel;
        [target.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"顶部背景白色.png"] forBarMetrics:UIBarMetricsDefault];
    }
    
    
}

+ (void)customizeNavBar:(UIViewController* )target withTView:(UIView* )title buttonImgL:(UIView* )left buttonImgR:(UIView* )right
{
    
    if (left)
    {
        
        UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithCustomView:left];
        target.navigationItem.leftBarButtonItem = leftItem;
    }
    if (right)
    {
        UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:right];
        target.navigationItem.rightBarButtonItem = rightItem;
    }
    if (title)
    {
        target.navigationItem.titleView = title;
    }
}


+ (void)xhNavBar:(UIViewController *)target title:(UILabel *)titleView leftBtn:(UIButton *)leftBtn leftImage:(UIImage *)leftImage rightBtn:(UIButton *)rightBtn rightImage:(UIImage *)rightImage
{
    CGFloat fontSize = 20;
    if(leftBtn)
    {
        if(leftImage)
        {
            CGSize sizeTemp = leftImage.size;
            leftBtn.frame = CGRectMake(0, 0, 60, 44);
            CGFloat top = (44 -fontSize)/2;
            CGFloat left = 60 - fontSize*sizeTemp.width/sizeTemp.height;
            [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(top, 0, top, left)];
            [leftBtn setImage:leftImage forState:UIControlStateNormal];
        }
        else
        {
            [leftBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 15)];
        }
        UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
        target.navigationItem.leftBarButtonItem = leftItem;
    }
    
    if(rightBtn)
    {
        if(rightImage)
        {
            CGSize sizeTemp = rightImage.size;
            rightBtn.frame = CGRectMake(0, 0, 44, 44);
            CGFloat top = (44 -fontSize)/2;
            CGFloat left = 44 - fontSize*sizeTemp.width/sizeTemp.height;
            [rightBtn setImageEdgeInsets:UIEdgeInsetsMake(top, left, top, 0)];
            [rightBtn setImage:rightImage forState:UIControlStateNormal];
        }
        else
        {
            [rightBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, -10)];
        }
        
        UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        target.navigationItem.rightBarButtonItem = rightItem;
    }
    
    if(titleView)
    {
        CGFloat titleWidth = 0;
        if(rightBtn||leftBtn)
        {
            CGFloat widthTemp = rightBtn?(leftBtn?(leftBtn.frame.size.width>=rightBtn.frame.size.width?leftBtn.frame.size.width:rightBtn.frame.size.width):rightBtn.frame.size.width):leftBtn.frame.size.width;
            titleWidth = target.navigationController.navigationBar.frame.size.width-2*widthTemp-10;
        }
        else
        {
            titleWidth =target.navigationController.navigationBar.frame.size.width-80;
        }
        titleView.frame = CGRectMake(0, 0, 100., target.navigationController.navigationBar.frame.size.height);
        target.navigationItem.titleView = titleView;
    }
}

@end
